﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LeftController : MonoBehaviour
{
	public UnityEngine.UI.RawImage CenterPoint;
	public UnityEngine.UI.RawImage BG;

	public bool isFixCenter = true;

	public Vector2 MoveCenter;

	float m_MaxRadius = 145.0f;
	int moveFingerID = -1;
	bool lastistouch = false;
	bool m_IsEntered = false;
	bool m_inOrg = false;	//当前的位置已经在原始位置了
	public System.Action<Vector2> Move;

	public const float m_toOrgTime = 0.2f;

	public float fingeUpTime = 0.0f;

	public Vector2 orgPos;	//初始位置


    public static LeftController instance = null;
    void Awake()
    {
        instance = this;
    }

	// Use this for initialization
	void Start()
	{
		SetExit();
		//BG.rectTransform.localPosition = new Vector2(MoveCenter.x * 1280.0f, (MoveCenter.y) * 720.0f);
		orgPos = BG.rectTransform.position;
	}

	// Update is called once per frame
	void Update()
	{
		bool isinmove = false;
		UpdateInput(ref isinmove);
		if(!m_IsEntered)
		{
			if(!m_inOrg)
			{
				if (Time.time > fingeUpTime + m_toOrgTime)
				{
					BG.rectTransform.position = orgPos;
                    Debug.Log("BG.rectTransform.position " + BG.rectTransform.position.ToString());
					m_inOrg = true;
				}
			}
		}
	}

	void UpdateInput(ref bool isinmove)
	{
		int count = Input.touchCount;
		for (int i = 0; i < count; ++i)
		{
			ProcessTouch(Input.GetTouch(i), ref isinmove);
		}

		if (Input.GetMouseButton(0))
		{

			Touch t = new Touch();
			t.position = Input.mousePosition;
			t.fingerId = 9999;
			if (!lastistouch)
			{
				t.phase = TouchPhase.Began;
				lastistouch = true;
			}
			else
			{
				t.phase = TouchPhase.Moved;
			}
			ProcessTouch(t, ref isinmove);
		}
		if (lastistouch && !Input.GetMouseButton(0))
		{
			Touch t = new Touch();
			t.position = Input.mousePosition;
			t.fingerId = 9999;
			t.phase = TouchPhase.Ended;
			ProcessTouch(t, ref isinmove);
			lastistouch = false;
		}

		if (!lastistouch)
		{
			float x = 0.0f;
			float y = 0.0f;
			y = Input.GetAxis("Vertical");
			x = Input.GetAxis("Horizontal");
			if (x == 0.0f && y == 0.0f)
			{
				SetExit();
			}
			else
			{
				SetEnter();
				var pos = new Vector2(x, y);
				if (pos.sqrMagnitude > 1.0f)
				{
					pos = pos.normalized;
					x = pos.x;
					y = pos.y;
				}
				CenterPoint.rectTransform.localPosition = pos * m_MaxRadius;
			}
			if (Move != null)
			{
				Move(new Vector2(x, y));
			}
		}
	}




	Vector2 GetScreenPosition(Vector2 inputposition) //返回一个左下角是0.0f,0.0f  右上角是 1.0f,1.0f
	{
		float x = inputposition.x / (float)Screen.width;
		float y = inputposition.y / (float)Screen.height;
		return new Vector2(x, y);
	}




	void ProcessTouch(Touch touch, ref bool isinmove)
	{
		if(!m_enable)
		{
			isinmove = false;
			return;
		}
		//Vector2 p = GetScreenPosition(touch.position);
		if (touch.phase == TouchPhase.Began)
		{
			if (moveFingerID < 0 )
			{
				//先检测是不是在 有效区域内

				RectTransform rootRect = this.GetComponent<RectTransform>();

				if (RectTransformUtility.RectangleContainsScreenPoint(rootRect, touch.position))
				{
					if (Time.time > fingeUpTime + m_toOrgTime)
					{
						#region 重置中心值
						MoveCenter = touch.position;
						if (MoveCenter.x < m_MaxRadius * Screen.width / 1280.0f)
						{
							MoveCenter.x = m_MaxRadius * Screen.width / 1280.0f;
						}
						if (MoveCenter.y < m_MaxRadius * Screen.width / 1280.0f)
						{
							MoveCenter.y = m_MaxRadius * Screen.width / 1280.0f;
						}
						if (MoveCenter.x > (rootRect.rect.width - m_MaxRadius) * Screen.width / 1280.0f)
						{
							MoveCenter.x = (rootRect.rect.width - m_MaxRadius) * Screen.width / 1280.0f;
						}
						if (MoveCenter.y > (rootRect.rect.height - m_MaxRadius) * Screen.width / 1280.0f)
						{
							MoveCenter.y = (rootRect.rect.height - m_MaxRadius) * Screen.width / 1280.0f;
						}
						//检测是否超过标记范围
						BG.rectTransform.position = MoveCenter;
                        Debug.Log("BG.rectTransform.position " + BG.rectTransform.position.ToString());

                        #endregion
                        //
                    }
                    moveFingerID = touch.fingerId;
					SetEnter();
				}
			}
		}
		if (touch.phase == TouchPhase.Moved || touch.phase == TouchPhase.Stationary)
		{
			if (touch.fingerId == moveFingerID)
			{

				Vector2 pos = touch.position - MoveCenter;

				if (pos.sqrMagnitude > m_MaxRadius * m_MaxRadius)
				{
					pos = pos.normalized * m_MaxRadius;
				}

				CenterPoint.rectTransform.position = MoveCenter + pos;
				if (pos.sqrMagnitude < m_MaxRadius * m_MaxRadius * 0.01f)
				{
					pos = Vector2.zero;
				}
				else
				{
					pos.Normalize();

				}
				if (Move != null)
				{
					Move(pos);
				}
				if (pos.x != 0.0f || pos.y != 0.0f)
				{
					isinmove = true;
				}
				else
				{
					isinmove = false;
				}
			}
		}
		if (touch.phase == TouchPhase.Ended)
		{
			if (touch.fingerId == moveFingerID)
			{
				fingeUpTime = Time.time;
				moveFingerID = -1;
				if (Move != null)
					Move(Vector2.zero);
				CenterPoint.rectTransform.position = BG.rectTransform.position;
				SetExit();
			}
		}


	}

	void SetExit()
	{
		if (m_IsEntered)
		{
			BG.CrossFadeAlpha(0.2f, 0.2f, false);
			CenterPoint.CrossFadeAlpha(0.5f, 0.2f, false);
			CenterPoint.rectTransform.localPosition = Vector2.zero;
			m_IsEntered = false;
		}
	}

	void SetEnter()
	{
		if (!m_IsEntered)
		{
			m_inOrg = false;
			m_IsEntered = true;
			BG.CrossFadeAlpha(1.0f, 0.2f, false);
			CenterPoint.CrossFadeAlpha(1.0f, 0.2f, false);
		}
	}


	bool m_enable = true;

	public void SetEnable(bool isOn)
	{
		m_enable = isOn;
		if(!isOn)
		{
			fingeUpTime = Time.time;
			moveFingerID = -1;
			if (Move != null)
				Move(Vector2.zero);
			CenterPoint.rectTransform.position = BG.rectTransform.position;
			SetExit();
		}
	}


    public bool IsInMoving
    {
        get
        {
            return m_IsEntered;
        }
    }
}
